/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.BusinessLogic;

/**
 *
 * @author WEN
 */
import DAO.DAO;
import Exception.KwickKioskDBException;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.util.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.*;

public class LineChart {

    private DAO dao;

    public LineChart() {
        try {
            dao = new DAO();
        } catch (KwickKioskDBException ex) {
            Logger.getLogger(LineChart.class.getName()).log(Level.SEVERE, null,
                    ex);
        }
    }

    private JFreeChart createChart(XYDataset xydataset) {
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Sales Chart",
                "Period", "Amount",
                xydataset, true, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
        xyplot.setDomainCrosshairVisible(true);
        xyplot.setRangeCrosshairVisible(true);
        org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer =
                xyplot.getRenderer();
        if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer xylineandshaperenderer =
                    (XYLineAndShapeRenderer) xyitemrenderer;
            xylineandshaperenderer.setShapesVisible(true);
            xylineandshaperenderer.setShapesFilled(true);
            xylineandshaperenderer.setItemLabelsVisible(true);
        }
        return jfreechart;
    }

    private XYDataset createItemDataset(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != upcs.length; j++) {
            a = dao.getItemChartStat(upcs[j][0],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(upcs[j][1]);
            int day = 0;
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                StringTokenizer st = new StringTokenizer(a.get(i).get(2), "-");
                year = Integer.parseInt(st.nextToken());
                month = Integer.parseInt(st.nextToken());
                day = Integer.parseInt(st.nextToken());
                timeseries.add(new Day(day, month, year), Double.parseDouble(a.get(
                        i).get(3)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createItemPanel(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createItemDataset(upcs,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createItemMthDataset(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != upcs.length; j++) {
            a = dao.getItemChartMthStat(upcs[j][0],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(upcs[j][1]);
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                month = Integer.parseInt(a.get(i).get(2));
                year = Integer.parseInt(a.get(i).get(3));
                timeseries.add(new Month(month, year), Double.parseDouble(a.get(
                        i).get(4)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createItemMthPanel(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createItemMthDataset(upcs,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createItemYearDataset(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != upcs.length; j++) {
            a = dao.getItemChartYearStat(upcs[j][0],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(upcs[j][1]);
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                year = Integer.parseInt(a.get(i).get(2));
                timeseries.add(new Year(year), Double.parseDouble(
                        a.get(i).get(3)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createItemYearPanel(String[][] upcs,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createItemYearDataset(upcs,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createStoreDataset(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != stores.length; j++) {
            a = dao.getStoreChartStat(stores[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(stores[j]);
            int day = 0;
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                StringTokenizer st = new StringTokenizer(a.get(i).get(1), "-");
                year = Integer.parseInt(st.nextToken());
                month = Integer.parseInt(st.nextToken());
                day = Integer.parseInt(st.nextToken());
                timeseries.add(new Day(day, month, year), Double.parseDouble(a.get(
                        i).get(2)));
            }
            timeseriescollection.addSeries(timeseries);
        }
        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createStorePanel(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createStoreDataset(stores,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createStoreMthDataset(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != stores.length; j++) {
            a = dao.getStoreChartMthStat(stores[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(stores[j]);
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                month = Integer.parseInt(a.get(i).get(1));
                year = Integer.parseInt(a.get(i).get(2));
                timeseries.add(new Month(month, year), Double.parseDouble(a.get(
                        i).get(3)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createStoreMthPanel(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createStoreMthDataset(stores,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createStoreYearDataset(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != stores.length; j++) {
            a = dao.getStoreChartYearStat(stores[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(stores[j]);
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                year = Integer.parseInt(a.get(i).get(1));
                timeseries.add(new Year(year), Double.parseDouble(
                        a.get(i).get(2)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createStoreYearPanel(String[] stores,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createStoreYearDataset(stores,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createAreaDataset(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != codes.length; j++) {
            a = dao.getAreaChartStat(codes[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(codes[j]);
            int day = 0;
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                StringTokenizer st = new StringTokenizer(a.get(i).get(1), "-");
                year = Integer.parseInt(st.nextToken());
                month = Integer.parseInt(st.nextToken());
                day = Integer.parseInt(st.nextToken());
                timeseries.add(new Day(day, month, year), Double.parseDouble(a.get(
                        i).get(2)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createAreaPanel(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createAreaDataset(codes,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createAreaMthDataset(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != codes.length; j++) {
            a = dao.getAreaChartMthStat(codes[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(codes[j]);
            int month = 0;
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                month = Integer.parseInt(a.get(i).get(1));
                year = Integer.parseInt(a.get(i).get(2));
                timeseries.add(new Month(month, year), Double.parseDouble(a.get(
                        i).get(3)));
            }
            timeseriescollection.addSeries(timeseries);
        }

        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    private XYDataset createAreaYearDataset(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {




        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        Vector<Vector<String>> a;
        TimeSeries timeseries;
        for (int j = 0; j != codes.length; j++) {
            a = dao.getAreaChartYearStat(codes[j],
                    startDate,
                    endDate);
            timeseries = new TimeSeries(codes[j]);
            int year = 0;
            for (int i = 0; i != a.size(); i++) {
                year = Integer.parseInt(a.get(i).get(1));
                timeseries.add(new Year(year), Double.parseDouble(
                        a.get(i).get(2)));
            }
            timeseriescollection.addSeries(timeseries);
        }
        timeseriescollection.setDomainIsPointsInTime(true);
        timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
        return timeseriescollection;
    }

    public JPanel createAreaYearPanel(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createAreaYearDataset(codes,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }

    public JPanel createAreaMthPanel(String[] codes,
            String startDate,
            String endDate)
            throws KwickKioskDBException {

        JFreeChart jfreechart = createChart(createAreaMthDataset(codes,
                startDate,
                endDate));
        return new ChartPanel(jfreechart);
    }
}
